package com.wcity.manage.dao.dal.model;

import com.wcity.manage.common.context.Constants;

/**
 * 分页查询工具实体类
 * 
 * @author dy
 *
 * @param <T>
 */
public class PageModel<T extends AbstractModel> extends AbstractModel {
	
	private static final long serialVersionUID = 2385350032270583782L;
	
	private int pageNum;		// 页码
    private int numPerPage;		// 每页数据量
	private T params;			// 查询参数Model

	private int pageStart;		// 页起始坐标
    private int pageEnd;		// 页结束坐标

	private long total;			// 数据总量
	private int pageCount;      // 总页数

	public PageModel() {
		this.pageNum = 1;
		this.numPerPage = Constants.DEFAULT_PAGE_LEN;
		this.pageStart = 0;
		this.pageEnd = pageStart + numPerPage;
		this.params = null;
	}

    public PageModel( int pageNum, int numPerPage, T params ) {
    	
        this.pageNum = pageNum;
        this.numPerPage = numPerPage;
        this.pageStart = pageNum > 0 ? ( pageNum - 1 ) * numPerPage : 0;
        this.pageEnd = pageStart + numPerPage;
        this.params = params;
    }

	/* ------------------------------ Setter ------------------------------ */

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
		setStartAndEnd();
	}
	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
		setStartAndEnd();
	}
	public void setPageStart(int pageStart) {
		this.pageStart = pageStart;
	}
	public void setPageEnd(int pageEnd) {
		this.pageEnd = pageEnd;
	}
	public void setTotal( long total ) {
		if ( total < 0 ) {
			this.total = 0;
			this.pageCount = 0;
		}
		this.total = total;
		int y = (int) ( total % numPerPage );
		this.pageCount = y == 0 ? (int)( total / numPerPage) : (int)( ( total - y ) / numPerPage + 1 );
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public void setParams(T params) {
		this.params = params;
	}

	/* ------------------------------ Getter ------------------------------ */

	public long getTotal() {
		return total;
	}
	public int getPageNum() {
		return pageNum;
	}
	public int getNumPerPage() {
		return numPerPage;
	}
	public int getPageStart() {
		return pageStart;
	}
	public int getPageEnd() {
		return pageEnd;
	}
	public int getPageCount() {
		return pageCount;
	}
	public T getParams() {
		return params;
	}

	/**
	 * 计算页起始坐标&页结束坐标
	 */
	private void setStartAndEnd() {
		this.pageStart = pageNum > 0 ? ( pageNum - 1 ) * numPerPage : 0;
		this.pageEnd = pageStart + numPerPage;
	}

	@Override
	public String toString() {
		return "PageModel [pageNum=" + pageNum + ", numPerPage="
				+ numPerPage + ", pageStart=" + pageStart + ", total=" + total
				+ ", params=" + params + "]";
	}
	
}
